﻿using System;
using System.Collections.Generic;

namespace NDK.Motion.Card
{

    [Serializable]
    public class AdlinkIOCard : IOCard
    {
        private static log4net.ILog traceLogger = log4net.LogManager.GetLogger("AdlinkIOCard");
        private string _cardName;
        private int _cardID;
        [NonSerialized]
        private bool _Initialized;
        public bool Initialized
        {
            get { return _Initialized; }
            set { _Initialized = value; }
        }
        private string _adlinkIOCardType;
        private int _inputCount;
        private int _outputCount;
        public int CardID
        {
            get
            {
                return _cardID;
            }

            set
            {
                _cardID = value;
            }
        }

        public string CardName
        {
            get
            {
                return _cardName;
            }

            set
            {
                _cardName = value;
            }
        }

        public string CardType
        {
            get
            {
                return _adlinkIOCardType;
            }

            set
            {
                _adlinkIOCardType = value;
            }
        }


        public int InputCount
        {
            get
            {
                return _inputCount;
            }

            set
            {
                _inputCount = value;
            }
        }

        public int OutputCount
        {
            get
            {
                return _outputCount;
            }

            set
            {
                _outputCount = value;
            }
        }

        public bool CardInit(ref List<ICard> ioCards, int mode = 0)
        {
            for (ushort i = 0; i < 16; i++)
            {
                try
                {
                    DASK.Release_Card((ushort)_cardID);//先关闭卡片
                }
                catch { }
                //PCI_7230 
                if (DASK.Register_Card(DASK.PCI_7230, i) >= 0)
                {
                    string cardType = "PCI_7230";
                    int cardID = DASK.Register_Card(DASK.PCI_7230, Convert.ToUInt16(i)); //实列化卡片
                    AdlinkIOCard card = new AdlinkIOCard();
                    card.CardID = cardID;
                    card.CardType = cardType;
                    card.CardName = cardType;
                    card.InputCount = 32;
                    card.OutputCount = 32;
                    card.Initialized = true;
                    ioCards.Add(card);

                }
                //PCI_7432 
                if (DASK.Register_Card(DASK.PCI_7432, i) >= 0)
                {
                    string cardType = "PCI_7432";
                    int cardID = DASK.Register_Card(DASK.PCI_7432, Convert.ToUInt16(i)); //实列化卡片
                    AdlinkIOCard card = new AdlinkIOCard();
                    card.CardID = cardID;
                    card.CardType = cardType;
                    card.CardName = cardType;
                    card.InputCount = 32;
                    card.OutputCount = 32;
                    card.Initialized = true;
                    ioCards.Add(card);
                }
                //PCI_7442 
                if (DASK.Register_Card(DASK.PCI_7442, i) >= 0)
                {
                    string cardType = "PCI_7442";
                    int cardID = DASK.Register_Card(DASK.PCI_7442, Convert.ToUInt16(i)); //实列化卡片
                    AdlinkIOCard card = new AdlinkIOCard();
                    card.CardID = cardID;
                    card.CardType = cardType;
                    card.CardName = cardType;
                    card.InputCount = 64;
                    card.OutputCount = 64;
                    card.Initialized = true;
                    ioCards.Add(card);
                }
            }
            return true;
        }
        public bool CardInit(int mode = 0)
        {
            try
            {
                try
                {
                    DASK.Release_Card((ushort)_cardID);//先关闭卡片
                }
                catch { }
                switch (this.CardType)
                {
                    case "PCI_7230":
                        if (DASK.Register_Card(DASK.PCI_7230, (ushort)_cardID) >= 0)
                        {
                            int cardID = DASK.Register_Card(DASK.PCI_7230, Convert.ToUInt16(_cardID)); //实列化卡片
                            this.Initialized = true;
                            return true;
                        }
                        break;
                    case "PCI_7432":
                        if (DASK.Register_Card(DASK.PCI_7432, (ushort)_cardID) >= 0)
                        {
                            int cardID = DASK.Register_Card(DASK.PCI_7432, Convert.ToUInt16(_cardID)); //实列化卡片
                            this.Initialized = true;
                            return true;
                        }
                        break;
                    case "PCI_7442":

                        if (DASK.Register_Card(DASK.PCI_7442, (ushort)_cardID) >= 0)
                        {
                            int cardID = DASK.Register_Card(DASK.PCI_7442, Convert.ToUInt16(_cardID)); //实列化卡片
                            this.Initialized = true;
                            return true;
                        }
                        break;
                }
            }
            catch (Exception)
            {

                throw;
            }

            return false;
        }
        public bool GetDI(int port, int group = 0)
        {
            bool ret = false;
            ushort inputValue = 0;
            try
            {
                if (port >= 32)
                {
                    group = 1;
                    port = port - 32;
                }
                if (DASK.DI_ReadLine((ushort)CardID, (ushort)group, (ushort)port, out inputValue) == 0)//读取成功
                {
                    if (inputValue == 0)
                    {
                        ret = false;
                    }
                    else
                    {
                        ret = true;
                    }
                }
                else//读取失败
                {
                    throw new Exception("DASK.DO_ReadLine Err!");
                }
            }
            catch (Exception ex)
            {
                throw new Exception("DASK.DO_ReadLine Exception!" + ex.StackTrace);
            }
            return ret;
        }

        public bool GetDO(int port, int group = 0)
        {
            ushort outputValue = 0;
            bool ret = false;
            try
            {
                if (port >= 32)
                {
                    group = 1;
                    port = port - 32;
                }
                if (DASK.DO_ReadLine((ushort)CardID, (ushort)group, (ushort)port, out outputValue) == 0)//读取成功
                {
                    if (outputValue == 0)
                    {
                        ret = false;
                    }
                    else
                    {
                        ret = true;
                    }
                }
                else//读取失败
                {
                    throw new Exception("DASK.DO_ReadLine Err!");
                }
            }
            catch (Exception ex)
            {
                throw new Exception("DASK.DO_ReadLine Exception!" + ex.StackTrace);
            }
            return ret;
        }

        public bool SetDO(int port, int ONOrOFF, int group = 0)
        {
            try
            {
                if (port >= 32)
                {
                    group = 1;
                    port = port - 32;
                }
                int ret = DASK.DO_WriteLine((ushort)CardID, (ushort)group, (ushort)port, (ushort)ONOrOFF);
                if (ret != 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("DASK.DO_WriteLine Err!" + ex.StackTrace);
            }
        }

        public bool Finalize()
        {
            try
            {
                if (DASK.Release_Card((ushort)_cardID) == 0)
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("DASK.DO_ReadLine Exception!" + ex.StackTrace);
            }
            return false;
        }

        public void UpdateSlaveResource(ref ICard card, int mode = 0)
        {
            throw new NotImplementedException();
        }
    }
}
